﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "worker.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Worker *worker = new Worker;
    //调用moveToThread将该任务交给workThread
    worker->moveToThread(&workerThread);
    //operate信号发射后启动线程工作
    connect(this,&MainWindow::operate,worker,&Worker::doWork);
    //该线程结束时销毁
    connect(&workerThread,&QThread::finished,worker,&QObject::deleteLater);
    //线程结束后发送信号，对结果进行处理
    connect(worker,&Worker::resultReady,this,&MainWindow::handleResults);
    //启动线程
    workerThread.start();
    //发射信号，开始执行
   qDebug()<<"emit the signal to execute!";
   qDebug()<<" mainwindow.cpp current thread ID:"<<QThread::currentThreadId()<<'\n';
   emit operate(0);

}

MainWindow::~MainWindow()
{
    delete ui;
    workerThread.quit();
    workerThread.wait();
}

void MainWindow::on_pushButton_clicked()
{
    ui->textBrowser->setText(res);
}
